home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Tele / Pete Johnson / mehit 3.0.b15<source>.cpt / Centering Unit < prev    next >
Text File  |  1991-06-22  |  3KB  |  113 lines

  1. {Written in Lightspeed Pascal}
  2.  
  3. {This unit was written by Andrew Welch (A.Welch1 on GEnie)}
  4. {It is completely free for all to use, but the author assumes}
  5. {no responsibility for any bugs in this code.  It has been tested}
  6. {and used quite extensively, but you never know!}
  7.  
  8. {This unit consist of one procedure that your program 'sees'}
  9. {called CenterDLOG.  You pass CenterDLOG a DialogPtr or a}
  10. {WindowPtr and CenterDLOG centers the dialog or window over}
  11. {the frontmost window, or if there are no windows, it is just}
  12. {centered on the screen.}
  13.  
  14. {The purpose of the unit is to provide some standard way that Mac}
  15. {programmers out there can use to implement this convenient feature}
  16. {in all of their programs.  This routine is great for users who have big}
  17. {screens and hate to have to move their mouse all over the place to}
  18. {answer a dialog, because the dialogs are centered over the current}
  19. {(frontmost) window, where they are most likely to be working.}
  20. {It is also great for those people who have more than one monitor,}
  21. {as the dialogs are centered on the screen they are working on.}
  22.  
  23. {This routine is also great for you programmer types, because you}
  24. {don't have to worry about where the dialogs appear ("Will this fit on}
  25. {a Mac SE's screen?!?!?"), just call this routine and they will come}
  26. {up looking pretty no matter what computer the user is running.}
  27. {CenterDLOG is smart enough to make sure that the dialog fits on the}
  28. {screen after it is centered.}
  29.  
  30. {Here's an example of how you could use it: (note that although I}
  31. {refer to Dialogs here, the same can be done with Windows.}
  32.  
  33. {    MyDialog:=GetNewDialog(…);     The dialog should NOT be marked as visible!    }
  34. {    CenterDLOG(MyDialog);            …this prevents the user from seeing the dialog}
  35. {    ShowWindow(MyDialog);                jumping all over the screen}
  36.  
  37. {Quick, easy, and worth it.  Enjoy!}
  38.  
  39. unit Centerer;
  40.  
  41. interface
  42.  
  43. procedure CenterDLOG (adialog: dialogptr);
  44.  
  45. implementation
  46.  
  47. function GetGreyRgn: rgnhandle;
  48.     var
  49.         ourrgn: rgnhandle;
  50.         thePtr: ^rgnhandle;
  51.  
  52.     begin
  53.         thePtr := pointer($9EE);
  54.         ourrgn := thePtr^;
  55.         getgreyrgn := ourRgn;
  56.     end;
  57.  
  58. procedure CenterDlog;
  59.  
  60.     var
  61.         wmport, saveport: grafptr;
  62.         dwidth, dheight, wheight, wwidth, xplus, yplus, x, y: integer;
  63.         arect: rect;
  64.         thergn: rgnhandle;
  65.         thewindow: windowptr;
  66.  
  67.     begin
  68.         thewindow := frontwindow;
  69.         getwmgrport(wmport);
  70.         if thewindow <> nil then
  71.             begin
  72.                 arect := thewindow^.portrect;
  73.                 getport(saveport);
  74.                 setport(thewindow);
  75.                 localtoglobal(arect.topleft);
  76.                 xplus := arect.left;
  77.                 yplus := arect.top;
  78.                 setport(saveport);
  79.                 wwidth := thewindow^.portrect.right - thewindow^.portrect.left;
  80.                 wheight := thewindow^.portrect.bottom - thewindow^.portrect.top;
  81.             end
  82.         else
  83.             begin
  84.                 xplus := 0;
  85.                 yplus := 0;
  86.                 wwidth := wmport^.portrect.right - wmport^.portrect.left;
  87.                 wheight := wmport^.portrect.bottom - wmport^.portrect.top;
  88.             end;
  89.         arect := adialog^.portrect;
  90.         insetrect(arect, -5, -5);
  91.         dwidth := arect.right - arect.left;
  92.         dheight := arect.bottom - arect.top;
  93.         x := (wwidth - dwidth) div 2;
  94.         y := (wheight - dheight) div 2;
  95.         x := x + xplus + 5;
  96.         y := y + yplus + 5;
  97.         thergn := getgreyrgn;
  98.         xplus := (x + dwidth) - thergn^^.rgnbbox.right;
  99.         if xplus > 0 then
  100.             x := x - xplus;
  101.         xplus := x;
  102.         if xplus < 10 then
  103.             x := 10;
  104.         yplus := (y + dheight) - thergn^^.rgnbbox.bottom;
  105.         if yplus > 0 then
  106.             y := y - yplus;
  107.         yplus := y;
  108.         if yplus < 30 then
  109.             y := 30;
  110.         movewindow(adialog, x, y, false);
  111.     end;
  112.  
  113. end.